<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActionController::UrlFor</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActionController::UrlFor 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/actionpack/lib/action_controller/metal/url_for_rb.html">actionpack/lib/action_controller/metal/url_for.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Includes <code>url_for</code> into the host class. The class has to provide
a <code>RouteSet</code> by implementing the <code>_routes</code> method.
Otherwise, an exception will be raised.</p>

<p>In addition to <code>AbstractController::UrlFor</code>, this module
accesses the HTTP layer to define url options like the <code>host</code>.
In order to do so, this module requires the host class to implement
<code>env</code> and <code>request</code>, which need to be a
Rack-compatible.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">RootUrl</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActionController</span><span class="ruby-operator">::</span><span class="ruby-constant">UrlFor</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Rails</span>.<span class="ruby-identifier">application</span>.<span class="ruby-identifier">routes</span>.<span class="ruby-identifier">url_helpers</span>

  <span class="ruby-identifier">delegate</span> :<span class="ruby-identifier">env</span>, :<span class="ruby-identifier">request</span>, <span class="ruby-identifier">to</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">controller</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">controller</span>)
    <span class="ruby-ivar">@controller</span> = <span class="ruby-identifier">controller</span>
    <span class="ruby-ivar">@url</span>        = <span class="ruby-identifier">root_path</span> <span class="ruby-comment"># named route from the application.</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>U</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="UrlFor.html#method-i-url_options">url_options</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  
    <!-- Includes -->
    <div class="sectiontitle">Included Modules</div>
    <ul>
      
        <li>
          
            <a href="../AbstractController/UrlFor.html">
              AbstractController::UrlFor
            </a>
          
        </li>
      
    </ul>
  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-url_options">
            
              <b>url_options</b>()
            
            <a href="UrlFor.html#method-i-url_options" name="method-i-url_options" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-url_options_source')" id="l_method-i-url_options_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/57cfa7a98fee14c7b61f5d4930ca05ae262e9a91/actionpack/lib/action_controller/metal/url_for.rb#L25" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-url_options_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_controller/metal/url_for.rb, line 25</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">url_options</span>
  <span class="ruby-ivar">@_url_options</span> <span class="ruby-operator">||=</span> <span class="ruby-keyword">super</span>.<span class="ruby-identifier">reverse_merge</span>(
    <span class="ruby-value">:host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">host</span>,
    <span class="ruby-value">:port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">optional_port</span>,
    <span class="ruby-value">:protocol</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">protocol</span>,
    <span class="ruby-value">:_recall</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">symbolized_path_parameters</span>
  ).<span class="ruby-identifier">freeze</span>

  <span class="ruby-keyword">if</span> (<span class="ruby-identifier">same_origin</span> = <span class="ruby-identifier">_routes</span>.<span class="ruby-identifier">equal?</span>(<span class="ruby-identifier">env</span>[<span class="ruby-string">&quot;action_dispatch.routes&quot;</span>])) <span class="ruby-operator">||</span>
     (<span class="ruby-identifier">script_name</span> = <span class="ruby-identifier">env</span>[<span class="ruby-node">&quot;ROUTES_#{_routes.object_id}_SCRIPT_NAME&quot;</span>]) <span class="ruby-operator">||</span>
     (<span class="ruby-identifier">original_script_name</span> = <span class="ruby-identifier">env</span>[<span class="ruby-string">'ORIGINAL_SCRIPT_NAME'</span>])

    <span class="ruby-ivar">@_url_options</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">tap</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">options</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">original_script_name</span>
        <span class="ruby-identifier">options</span>[<span class="ruby-value">:original_script_name</span>] = <span class="ruby-identifier">original_script_name</span>
      <span class="ruby-keyword">else</span>
        <span class="ruby-identifier">options</span>[<span class="ruby-value">:script_name</span>] = <span class="ruby-identifier">same_origin</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">request</span>.<span class="ruby-identifier">script_name</span>.<span class="ruby-identifier">dup</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">script_name</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">options</span>.<span class="ruby-identifier">freeze</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-ivar">@_url_options</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    